package com.ruoyi.project.museum.exhibit.controller;

import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.museum.exhibit.domain.Exhibit;
import com.ruoyi.project.museum.exhibit.service.IExhibitService;

/**
 * 文物管理Controller
 * 
 * @author ruoyi
 * @date 2021-03-10
 */
@Controller
@RequestMapping("/museum/exhibit")
public class ExhibitController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(ExhibitController.class);
    private String prefix = "museum/exhibit";

    @Autowired
    private IExhibitService exhibitService;

    @RequiresPermissions("museum:exhibit:view")
    @GetMapping()
    public String exhibit()
    {
        return prefix + "/exhibit";
    }

    /**
     * 查询文物管理列表
     */
    @RequiresPermissions("museum:exhibit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Exhibit exhibit)
    {
        startPage();
        List<Exhibit> list = exhibitService.selectExhibitList(exhibit);
        return getDataTable(list);
    }

    /**
     * 导出文物管理列表
     */
    @RequiresPermissions("museum:exhibit:export")
    @Log(title = "文物管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Exhibit exhibit)
    {
        List<Exhibit> list = exhibitService.selectExhibitList(exhibit);
        ExcelUtil<Exhibit> util = new ExcelUtil<Exhibit>(Exhibit.class);
        return util.exportExcel(list, "exhibit");
    }

    /**
     * 新增文物管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文物管理
     */
    @RequiresPermissions("museum:exhibit:add")
    @Log(title = "文物管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Exhibit exhibit, @RequestParam("file") MultipartFile file) {
        try {
            String url = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            exhibit.setImg(url);
        } catch (Exception e) {
            log.error("批量上传失败！", e);
            return error(e.getMessage());
        }
        return toAjax(exhibitService.insertExhibit(exhibit));
    }

    /**
     * 修改文物管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Exhibit exhibit = exhibitService.selectExhibitById(id);
        mmap.put("exhibit", exhibit);
        return prefix + "/edit";
    }

    /**
     * 修改保存文物管理
     * @throws IOException 
     */
    @RequiresPermissions("museum:exhibit:edit")
    @Log(title = "文物管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Exhibit exhibit, @RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty()) {
    		String url = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
    		exhibit.setImg(url);
    	}
        return toAjax(exhibitService.updateExhibit(exhibit));
    }

    /**
     * 删除文物管理
     */
    @RequiresPermissions("museum:exhibit:remove")
    @Log(title = "文物管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(exhibitService.deleteExhibitByIds(ids));
    }
}
